package com.zq.designpaterns.other.servicelocator;

import java.util.ArrayList;
import java.util.List;

/**
 * ================================================
 * 作    者：程志强
 * 邮    箱：cheng.zhiqiang@cesgroup.com.cn
 * 版    本：1.0.0
 * 创建日期：2021/01/28  14:44
 * 修改日期：
 * 描    述：
 * 修订历史：
 * ================================================
 */
public class Cache {
    private final List<Service> services;

    public Cache() {
        services = new ArrayList<>();
    }

    public Service getService(String serviceName) {
        for (Service service : services) {
            if (service.getName().equalsIgnoreCase(serviceName)) {
                System.out.println("Returning cached  " + serviceName + " object");
                return service;
            }
        }
        return null;
    }

    public void addService(Service service) {
        boolean isExist = false;
        for (Service s : services) {
            if (s.getName().equalsIgnoreCase(service.getName())) {
                isExist = true;
            }
        }
        if (!isExist) {
            services.add(service);
        }
    }
}
